<template>
  <div class="container">
    <div class="top">
      <el-input style="width: 200px; margin-bottom: 5px" v-model="search.searchCode" placeholder="请输入商品名称或编码"
        clearable></el-input>
      <el-button size="small" type="primary" class="handleSearch" icon="el-icon-search"
        @click="handleSearch()">查询</el-button>
      <el-button type="info" size="small" icon="el-icon-tickets" @click="derive()">导出</el-button>
    </div>
    <el-table max-height="600" :data="tableData" border style="width: 100%" empty-text="暂无数据">
      <el-table-column prop="Name" label="商品名称" align="center"></el-table-column>
      <el-table-column prop="BarCode" label="条形码" align="center"></el-table-column>
      <el-table-column prop="Code" label="商品Code" align="center"></el-table-column>
      <el-table-column prop="ClassName" label="分类" align="center"></el-table-column>

      <el-table-column prop="ClassCode" label="分类编码" align="center"></el-table-column>
      <el-table-column prop="SalePrice" label="档案价格" align="center"></el-table-column>
      <el-table-column prop="percentageMsg" label="加价" align="center"></el-table-column>
      <el-table-column prop="CreateTime" label="添加时间" align="center"></el-table-column>
      <el-table-column prop="SupplierName" label="供应商名称" align="center"></el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope)">设置商品加价</el-button>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEditCooked(scope)">设置面食配料</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <addPercentageDialog @upData="getData" :dialog="dialog"></addPercentageDialog>
    <pastaDialog @upData="getData" :dialog="cookedDialog"></pastaDialog>
  </div>
</template>
<script>
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import { selectErpGoodsList } from "@/api/commodity/commodityList";
import AddPercentageDialog from "@/components/commodity/addPercentageDialog";
import PastaDialog from "@/components/commodity/pastaDialog";

import { getToken } from "@/utils/auth";

export default {
  components: {
    pagination: Pagination,
    addPercentageDialog: AddPercentageDialog,
    pastaDialog: PastaDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      search: {
        searchCode: "",
      },
      dialog: {
        centerDialogVisible: false,
        code: "",
        id: "",
      },
      cookedDialog: {
        centerDialogVisible: false,
        code: "",
        goods_bar_code: "",
        goods_name: "",
        num: "",
        unit: "",
      },
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        goods_code: this.search.searchCode,
        // category_code: "18,57,91",
        category_code: "1102,1103,1104",
        ismachining: 1,

      };
      selectErpGoodsList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((result) => {
              result.percentageMsg =
                result.type == 2 ? result.percentage + "%" : result.percentage;
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //设置商品加价
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.code = scope.row.Code;
      this.dialog.id = scope.row.ProductId;
    },
    //设置面食配料
    handleEditCooked(scope) {
      this.cookedDialog.centerDialogVisible = true;
      this.cookedDialog.code = scope.row.Code;
      this.cookedDialog.goods_bar_code = scope.row.BarCode;
      this.cookedDialog.goods_name = scope.row.Name;
      this.cookedDialog.unit = scope.row.Unit;
    },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/goods/exportErpGoodsList?token=" +
            getToken() +
            "&goods_code=" +
            this.search.searchCode +
            "&ismachining=1" +
            "&category_code=1102,1103,1104";
          // "&category_code=18,57";
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}

.pagination {
  text-align: right;
  padding: 10px;
}
</style>